package com.maltys.greedy;

import java.util.Arrays;

/**
 * @author malty
 * @link <a href="https://leetcode.cn/problems/candy/">分发糖果</a>
 */
public class Candy {
    public int candy(int[] ratings) {
        int[] cnt = new int[ratings.length];
        Arrays.fill(cnt, 1);
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i - 1] < ratings[i]) {
                cnt[i] = 1 + cnt[i - 1];
            }
        }
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i + 1] < ratings[i]) {
                cnt[i] = Math.max(1 + cnt[i + 1], cnt[i]);
            }
        }
        int sum = 0;
        for (int rating : cnt) {
            sum += rating;
        }
        return sum;
    }

    public static void main(String[] args) {
        Candy candy = new Candy();
        int sum = candy.candy(new int[]{1, 3, 4, 5, 2});
        System.out.println(sum);
    }
}
